const {
    getAllLaunches,
    addNewLaunch,
    existsLaunchWithId,
    abortLaunchById,
   } = require('../../models/launches.model');
function httpGetAllLaunches(req, res) {
    return res.status(200).json(getAllLaunches());
   }

   function httpAddNewLaunch(req, res) {
    const launch = req.body;
    if (!launch.mission || !launch.rocket || !launch.launchDate
         || !launch.target) {
         return res.status(400).json({
         error: '缺少必要的发射属性',
         });
         }
         addNewLaunch(launch);
         launch.launchDate = new Date(launch.launchDate);
         // 检查发射⽇期是否有效
         if (isNaN(launch.launchDate)) {
            return res.status(400).json({
                error: '发射⽇期⽆效',
                });
                }
               
                return res.status(201).json(launch);
               }
// 取消指定 ID 的发射任务
function httpAbortLaunch(req, res) {
    const launchId = Number(req.params.id);
    if (!existsLaunchWithId(launchId)) {
    return res.status(404).json({
    error: '发射任务不存在',
    });
    }
    const aborted = abortLaunchById(launchId);
    return res.status(200).json(aborted);
    }               
               
    module.exports = {
        httpGetAllLaunches,
        httpAddNewLaunch,
        httpAbortLaunch,
        };